package jam.network.message;

import jam.model.*;
import jam.network.*;
import java.util.*;

public class ClientActionRegister extends ClientCommand {
	protected static final String KEYWORD = "CA_REGISTER";

	private String email;
	private String password;

	@Override
	public void process(ClientController controller) {
		NetworkController network = controller.getNetworkController();
		ClientModel client = controller.getModel();
		boolean success = false;
		HashMap<String,String> credentials = new HashMap<String,String>();
		credentials.put("email",email);
		if(!client.isLoggedIn() && !User.exists(credentials)) {
			User newUser = new User(email, password);
			newUser.save();
			success = true;
		}
		controller.send(new ServerResponse(this, success));
	}

	@Override
	protected String getKeyword() {
		return ClientActionRegister.KEYWORD;
	}

	protected ClientActionRegister(int transactionId, Scanner args) {
		super(transactionId);
		this.email = args.next();
		this.password = args.next();
	}
}
